﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Elementum
{
    public class Switch : Actor
    {
        public Player player;
        public List<ChangeStateable> _targetList = new List<ChangeStateable>();
        public delegate void TouchActionHandler(TouchLocationState state);
        public event TouchActionHandler TouchStateAction;
        protected bool _isActive;
        protected Animation _switchOn;
        protected Animation _switchOff;
        protected Animation _buttonSprite;

        public Switch(World world, Stage stage)
            : base(world, stage)
        {
            _switchOn = new Animation("images/items/switchon", new Point(50, 50));
            _switchOff = new Animation("images/items/switchoff", new Point(50, 50));
            CurrentAnimation = _switchOn;
            _buttonSprite = new Animation("images/system/button", new Rectangle(2, 410, 100, 100), new Point(100, 100));
            _isActive = false;

        }

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            _body._userData = "Switch";
            _body._type = BodyType.Static;
        }

        public void AddTarget(ChangeStateable target)
        {
            _targetList.Add(target);
        }

        protected override void ConfigFilter()
        {
            base.ConfigFilter();
            _filter.categoryBits = GroundCategory;
            _filter.maskBits = GroundMask;
        }

        protected override void SetSize(Vector2 size)
        {
            base.SetSize(size);
            _fixture.SetSensor(true);
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            //if(CurrentAnimation!=null)
            //CurrentAnimation.LayerDepth = 0.8f;
        }

        public virtual void ChangeStage(VisibleObject owner, TouchLocationState state)
        {
            if (state == TouchLocationState.Pressed)
            {
                _isActive = !_isActive;
                MusicManager.PlayMusic("switch");
                foreach (ChangeStateable _s in _targetList)
                {
                    _s.ChangeState();
                }
            }
            if (TouchStateAction != null)
                TouchStateAction(state);
            if (_isActive)
                CurrentAnimation = _switchOff;
            else
                CurrentAnimation = _switchOn;

        }

        public override void Restart()
        {
            base.Restart();
            _isActive = false;
            if (_isActive)
                CurrentAnimation = _switchOff;
            else
                CurrentAnimation = _switchOn;
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player")
            {
                player = (Player)contactBody.Owner;
                player.ActionEvent += ChangeStage;
                GameStage.ActionButtom.Enable = true; 
                GameStage.ActionButtom.ButtonSprite = _buttonSprite;
            }
        }



        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player")
            {
                player.ActionEvent -= ChangeStage;
                GameStage.ActionButtom.Enable = false;
                GameStage.ActionButtom.ButtonSprite = null;
            }
        }
    }
}
